<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline='true'>
				<el-form-item>
					<el-input v-model="name" placeholder="账号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-plus" @click="handleAdd()"></el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" element-loading-text="拼命加载中" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="userId" label="账号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="username" label="昵称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="手机号码" width="180" sortable>
			</el-table-column>
			<el-table-column prop="mail" label="邮箱" width="180" sortable>
			</el-table-column>
			<el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" sortable>
			</el-table-column>
			<el-table-column prop="status" label="状态" width="100" :formatter="formatStatus" sortable>
			</el-table-column>
			<el-table-column label="操作" width="220">
				<template slot-scope="scope">
					<el-popover
						placement="right"
						width="400"
						trigger="click">
						<el-table :data="supplierMsg">
							<el-table-column width="150" property="supplierName" label="供应商名称"></el-table-column>
							<el-table-column width="300" property="address" label="地址"></el-table-column>
						</el-table>
						<el-button type="info" slot="reference" @click="handleMore(scope.$index, scope.row)">更多</el-button>
					</el-popover>
					<el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)"></el-button>
					<el-button type="danger" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)"></el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange" :page-size="pagination.pageSize" :total="pagination.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
			<el-form-item label="账号" prop="userId">
				<el-input v-model="editForm.userId" readonly></el-input>
			</el-form-item>
				<el-form-item label="昵称">
					<el-input v-model="editForm.username"></el-input>
				</el-form-item>
				<el-form-item label="手机号码">
					<el-input v-model="editForm.phone"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="mail">
					<el-input v-model="editForm.mail"></el-input>
				</el-form-item>
				<el-form-item label="性别">
					<el-select v-model="editForm.sex" clearable placeholder="请选择">
						<el-option
							v-for="item in sexOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-select v-model="editForm.status" clearable placeholder="请选择">
						<el-option
							v-for="item in statusOptions"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item label="账号" prop="userId">
					<el-input v-model="addForm.userId" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="昵称" prop="username">
					<el-input v-model="addForm.username"></el-input>
				</el-form-item>
				<el-form-item label="手机号码" prop="phone">
					<el-input v-model="addForm.phone"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="mail">
					<el-input v-model="addForm.mail"></el-input>
				</el-form-item>
				<el-form-item label="性别">
					<el-select v-model="addForm.sex" clearable placeholder="请选择">
						<el-option
							v-for="item in sexOptions2"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态">
					<el-select v-model="addForm.status" clearable placeholder="请选择">
						<el-option
							v-for="item in statusOptions2"
							:key="item.value"
							:label="item.label"
							:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="供应商名称" prop="supplierName">
					<el-input v-model="addForm.supplierName"></el-input>
				</el-form-item>
				<el-form-item label="供应商地址" prop="address">
					<el-input v-model="addForm.address"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { instance, getUserListPage, addSupplier, removeSupplier, editUser, getSupplierMsg } from '../../api';

	export default {
		data() {
			return {
				users: [],
				supplierMsg: [],
				name: '',
				edit: true,
				readonly: true,
				pagination: {
					total: 0,
					pageNum: 1,
					pageSize: 20,
				},
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					userId: [
						{ required: true, message: '请输入会员账号', trigger: 'blur' }
					],
					mail: [
			      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
			      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
			    ],
				},
				//编辑界面数据
				editForm: {
					userId: '',
					username: '',
					phone: '',
					mail: '',
					sex: -1,
					status: 0
				},
				sexOptions: [{
          value: 0,
          label: '女'
        }, {
          value: '1',
          label: '男'
        }],
				statusOptions: [{
          value: 1,
          label: '正常'
        }, {
          value: 9,
          label: '禁用'
        }],

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					userId: [
						{ required: true, message: '请输入账号名称', trigger: 'blur' }
					],
					mail: [
			      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
			      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
			    ],
					supplierName: [
						{ required: true, message: '请输入供应商名称', trigger: 'blur' }
					],
					address: [
						{ required: true, message: '请输入供应商地址', trigger: 'blur' }
					],
				},
				//新增界面数据
				addForm: {
					userId: '',
					username: '',
					phone: '',
				  mail: '',
					sex: -1,
					status: 0
				},

				sexOptions2: [{
          value: 0,
          label: '女'
        }, {
          value: '1',
          label: '男'
        }],
				statusOptions2: [{
          value: 1,
          label: '正常'
        }, {
          value: 9,
          label: '禁用'
        }],
			}
		},
		methods: {
			//状态显示转换
			formatStatus: function (row, column) {
				return row.status == 1 ? '正常' : row.status == 9 ? '禁用' : '未知';
			},
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(currentPage) {
				this.pagination.pageNum = currentPage;
				this.getUsers();
			},
			//获取api列表
			getUsers() {
				let params = {
					roleId: 104,
					name: this.name,
					currentPage: this.pagination.pageNum,
					pageSize: this.pagination.pageSize
				};
				console.log(params);
				this.listLoading = true;
				//NProgress.start();
				getUserListPage(params).then((res) => {
					this.pagination.total = res.data.data.users.total;
					this.users = res.data.data.users.list;
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let params = { userId: row.userId };
					removeSupplier(params).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					userId: '',
					username: '',
					phone: '',
					mail: '',
					sex: -1,
					status: 0,
				};
			},
			//更多
			handleMore: function(index, row) {
				let params = { userId: row.userId };
				getSupplierMsg(params).then((res) => {
					this.supplierMsg = res.data.data.msg
				});
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let params = Object.assign({}, this.editForm);
							editUser(params).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let params = Object.assign({}, this.addForm);
							addSupplier(params).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let params = { ids: ids };
					batchRemoveUser(params).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>
